package com.simon.app.october.modules.sys.controllers;

import com.simon.app.october.common.annotation.SysLog;
import com.simon.app.october.common.utils.Result;
import com.simon.app.october.common.validator.ValidatorUtils;
import com.simon.app.october.common.validator.group.AddGroup;
import com.simon.app.october.common.validator.group.UpdateGroup;
import com.simon.app.october.modules.sys.entity.SysRoleEntity;
import com.simon.app.october.modules.sys.service.SysRoleService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/sys/role")
public class SysRoleController extends AbstractController {

    @Autowired
    SysRoleService roleService;

    @GetMapping("/select")
    public Result select() {
        Map<String, Object> map = new HashMap<>();

        List<SysRoleEntity> list = roleService.getList();
        return Result.ok().put("list", list);
    }

    /**
     * 获取角色列表
     * */
    @GetMapping("/list")
    public Result list(@RequestParam Map<String, Object> params) {
        // 查询所有角色，分页
        return Result.ok().put("page", roleService.listPage(params));
    }

    @GetMapping("/info/{roleId}")
    public Result info(@PathVariable(name = "roleId") int roleId) {
        return Result.ok().put("role", roleService.findById(roleId));
    }

    @PostMapping("/save")
    @RequiresPermissions("sys:role:save")
    @SysLog("新增角色")
    public Result save(@RequestBody SysRoleEntity role) {
        ValidatorUtils.validateEntity(role, AddGroup.class);
        role.setCreateUserId(getUserId());
        role.setCreateTime(new Date());
        roleService.save(role);
        return Result.ok();
    }

    @PostMapping("/update")
    @RequiresPermissions("sys:role:update")
    @SysLog("更新角色")
    public Result update(@RequestBody SysRoleEntity role) {
        ValidatorUtils.validateEntity(role, UpdateGroup.class);
        roleService.update(role);
        return Result.ok();
    }

    @PostMapping("/delete")
    @RequiresPermissions("sys:role:delete")
    @SysLog("删除角色")
    public Result delete(@RequestBody Long[] ids) {

        roleService.delete(ids);
        return Result.ok();
    }
}
